//
//  LGTUserChoseVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/13.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTUserChoseVC: LGTBaseVC {
    
    var fromTabbarState:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: false, title: "选择身份", back: false)
        
        
        let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: CGFloat(KRate * 15), text: "请选择你的身份")
        self.view.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.height.equalTo(KRate * 15)
            make.left.equalTo(KRate * 31)
            make.top.equalTo(NavBarH + KRate * 39)
        }
        
        let sendTaskBtn:UIButton = UIButton.init()
        sendTaskBtn.layer.masksToBounds = true;
        sendTaskBtn.layer.cornerRadius = CGFloat(SizeFit(5))
        
        sendTaskBtn.backgroundColor = BaseUtil.baseUtil.getHexColor(colorHex: 0xFFB21A)
        self.view.addSubview(sendTaskBtn)
        sendTaskBtn.snp.makeConstraints { (make) in
            make.top.equalTo(descLabel.snp_bottomMargin).offset(KRate * 21)
            make.left.equalTo(KRate * 31)
            make.width.equalTo(KRate * 319)
            make.height.equalTo(KRate * 67)
        }
        
        
        let makeTaskBtn:UIButton = UIButton.init()
        makeTaskBtn.layer.masksToBounds = true;
        makeTaskBtn.layer.cornerRadius = CGFloat(SizeFit(5))
        self.view.addSubview(makeTaskBtn)
        makeTaskBtn.backgroundColor = BaseUtil.baseUtil.getHexColor(colorHex: 0x00C9E7)
        makeTaskBtn.snp.makeConstraints { (make) in
            make.top.equalTo(sendTaskBtn.snp_bottomMargin).offset(KRate * 21)
            make.left.equalTo(KRate * 31)
            make.width.equalTo(KRate * 319)
            make.height.equalTo(KRate * 67)
        }
        
        
        let icon1:UIImageView = LGTViewCreateUtil.share.createImageView(image: "advertisers_icon")
        self.view.addSubview(icon1)
        icon1.snp.makeConstraints { (make) in
            make.width.height.equalTo(KRate * 45)
            make.centerY.equalTo(sendTaskBtn)
            make.left.equalTo(sendTaskBtn).offset(KRate * 17)
        }
        
        let label1:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: UIColor.white, fontSize: CGFloat(15 * KRate), text: "我要发布任务")
        self.view.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.left.equalTo(icon1.snp_rightMargin).offset(KRate * 18)
            make.centerY.equalTo(icon1)
            make.height.equalTo(KRate * 15)
        }
        
        let gotoIcon1:UIImageView = LGTViewCreateUtil.share.createImageView(image: "go_icon")
        self.view.addSubview(gotoIcon1)
        gotoIcon1.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(20))
            make.height.equalTo(SizeFit(17))
            make.centerY.equalTo(icon1)
            make.right.equalTo(-SizeFit(46))
        }
        
        let icon2:UIImageView = LGTViewCreateUtil.share.createImageView(image: "advertisers_icon")
        self.view.addSubview(icon2)
        icon2.snp.makeConstraints { (make) in
            make.width.height.equalTo(KRate * 45)
            make.centerY.equalTo(makeTaskBtn)
            make.left.equalTo(sendTaskBtn).offset(KRate * 17)
        }
        
        let label2:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: UIColor.white, fontSize: CGFloat(15 * KRate), text: "我要做任务")
        self.view.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.left.equalTo(icon2.snp_rightMargin).offset(KRate * 18)
            make.centerY.equalTo(icon2)
            make.height.equalTo(KRate * 15)
        }
        
        let gotoIcon2:UIImageView = LGTViewCreateUtil.share.createImageView(image: "go_icon")
        self.view.addSubview(gotoIcon2)
        gotoIcon2.snp.makeConstraints { (make) in
            make.width.equalTo(SizeFit(20))
            make.height.equalTo(SizeFit(17))
            make.centerY.equalTo(icon2)
            make.right.equalTo(-SizeFit(46))
        }
        
        
        //        // 设置阴影偏移量
        //        // 设置阴影透明度
        //        // 设置阴影半径
        sendTaskBtn.layer.shadowColor = BaseUtil.baseUtil.getHexColor(colorHex: 0xFFB21A).cgColor
        sendTaskBtn.layer.shadowOffset = CGSize.init(width: 0, height: 0)
        sendTaskBtn.layer.shadowOpacity = 0.5
        sendTaskBtn.layer.shadowRadius = CGFloat(SizeFit(5))
        
        makeTaskBtn.layer.shadowColor = BaseUtil.baseUtil.getHexColor(colorHex: 0x00C9E7).cgColor
        makeTaskBtn.layer.shadowOffset = CGSize.init(width: 0, height: 0)
        makeTaskBtn.layer.shadowOpacity = 0.5
        makeTaskBtn.layer.shadowRadius = CGFloat(SizeFit(5))
        
        makeTaskBtn.addTarget(self, action: #selector(makeTask), for: UIControl.Event.touchUpInside)
        sendTaskBtn.addTarget(self, action: #selector(sendTask), for: UIControl.Event.touchUpInside)
    }
    
    
    @objc func sendTask(){
        self.request(user_type: 1)
    }
    
    @objc func makeTask(){
        self.request(user_type: 2)
    }
    
    func request(user_type:Int){
        let param:Dictionary<String, Any> = ["user_type": user_type]
        weak var weakSelf = self
        LGTHttpUtil.httpUtil.requestData(url: User_Chose_URL, param: param, showMessage: true, success: { (json) in
            //数据存储
            //LGTUserUtil.share.saveUserInfo(data: json["data"] ?? [:])
            //更新
            let userInfo:Dictionary<String, Any> = json["data"] as? Dictionary<String, Any> ?? [:]
            LGTUserUtil.share.updateUserInfo(data:  userInfo)
            //不是从主页进入的
            if weakSelf?.fromTabbarState == false{
                //跳转主页界面
                NotificationCenter.default.post(name: Login_Success_Note, object: nil, userInfo: nil)
            }else{
                weakSelf?.navigationController?.popViewController(animated: true)
            }
//            //更新
//            let userInfo:Dictionary<String, Any> = json["data"] as? Dictionary<String, Any> ?? [:]
//            LGTUserUtil.share.updateUserInfo(data:  userInfo)
            //发布角色变化消息
            NotificationCenter.default.post(name: User_Role_Change_Note, object: nil)
            
        }) { (json) in
            
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
}
